from apis.wework import WeWork
from utils.file_tools import FileTool


class TagManagement(WeWork):
    def __init__(self):
        super().__init__()
        # 读取secrets文件
        yaml_data = FileTool.read_yaml("secrets")
        corpid = yaml_data.get('corpid').get('companyid')
        corpsecret = yaml_data.get('corpsecret').get('secret')
        self.token = self.get_token(corpid,corpsecret)

    # 创建标签
    def create_tag(self, tagname, tagid=None):
        '''
        :param tagname:必填，标签名称，长度限制为32个字以内（汉字或英文字母），标签名不可与其他标签重名。
        :param tagid:非必填，标签id，非负整型，指定此参数时新增的标签会生成对应的标签id，不指定时则以目前最大的id自增。
        :return:{"errcode": 0,"errmsg": "created", "tagid": tagid}
        '''
        data = {
            "method":"POST",
            "url":f"https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token={self.token}",
            "json":{
                    "tagname": tagname,
                    "tagid": tagid
            }
        }
        return self.send(**data)

    # 更新标签名字
    def update_tag(self, tagid, tagname):
        '''
        :param tagid: 必填，标签ID
        :param tagname:必填，标签名称，长度限制为32个字（汉字或英文字母），标签不可与其他标签重名。
        :return:{"errcode": 0,"errmsg": "updated"}
        '''
        data = {
            "method": "POST",
            "url": f"https://qyapi.weixin.qq.com/cgi-bin/tag/update?access_token={self.token}",
            "json": {
                "tagid": tagid,
                "tagname": tagname
            }
        }
        return self.send(**data)

    # 删除标签
    def dalete_tag(self, tagid):
        '''
        :param tagid:必填，标签ID
        :return:{"errcode": 0,"errmsg": "deleted"}
        '''
        data = {
            "method": "GET",
            "url": f"https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token={self.token}",
            "params": {
                "tagid": tagid
            }
        }
        return self.send(**data)

    # 获取标签成员
    def get_tag_member(self, tagid):
        '''
        :param tagid:必填，标签ID
        :return:{"errcode": 0,"errmsg": "ok","tagname": "乒乓球协会","userlist": [{"userid": "zhangsan","name": "李四"}],"partylist": [2]}
        '''
        data = {
            "method": "GET",
            "url": f"https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token={self.token}",
            "params": {
                "tagid": tagid
            }
        }
        return self.send(**data)

    # 增加标签人员
    def add_tag_member(self, tagid, userlist=None, partylist=None):
        '''
        :param tagid:必填，标签ID
        :param userlist:非必填，企业成员ID列表，注意：userlist、partylist不能同时为空，单次请求个数不超过1000
        :param partylist:非必填，企业部门ID列表，注意：userlist、partylist不能同时为空，单次请求个数不超过100
        :return:
        1、正确时返回{"errcode": 0,"errmsg": "ok"}
        2、若部分userid、partylist非法，则返回：{"errcode": 0,"errmsg": "ok","invalidlist"："usr1|usr2|usr","invalidparty"：[2,4]}
        3、当包含userid、partylist全部非法时返回{"errcode": 40070,"errmsg": "all list invalid "}
        '''
        if partylist is None:
            partylist = [1]
        data = {
            "method": "POST",
            "url": f"https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers?access_token={self.token}",
            "json": {
                "tagid": tagid,
                "userlist": userlist,
                "partylist": partylist
            }
        }
        return self.send(**data)

    # 删除标签成员
    def delete_tag_member(self, tagid, userlist=None, partylist=None):
        '''
        :param tagid:必填，标签ID
        :param userlist:非必填，企业成员ID列表，注意：userlist、partylist不能同时为空，单次请求个数不超过1000
        :param partylist:非必填，企业部门ID列表，注意：userlist、partylist不能同时为空，单次请求个数不超过100
        :return:
        1、正确时返回{"errcode": 0,"errmsg": "deleted"}
        2、若部分userid、partylist非法，则返回：{"errcode": 0,"errmsg": "ok","deleted"："usr1|usr2|usr","invalidparty"：[2,4]}
        3、当包含userid、partylist全部非法时返回{"errcode": 40031,"errmsg": "all list invalid "}
        '''
        data = {
            "method": "POST",
            "url": f"https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers?access_token={self.token}",
            "json": {
                "tagid": tagid,
                "userlist": userlist,
                "partylist": partylist
            }
        }
        return self.send(**data)

    # 获取标签列表
    def get_tag_list(self):
        '''
        :return: {"errcode": 0,"errmsg": "ok","taglist":[{"tagid":1,"tagname":"a"},{"tagid":2,"tagname":"b"}]}
        '''
        data = {
            "method": "GET",
            "url": f"https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token={self.token}"
        }
        return self.send(**data)